package client

import (
	"context"
	"google.golang.org/grpc"
	"travel-app/common/proto/travel"
)

type Handlers func(ctx context.Context, in travel.TravelClient) (interface{}, error)

func TravelClient(ctx context.Context, handle Handlers) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := travel.NewTravelClient(dial)
	res, err := handle(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}
func EsSync(ctx context.Context, i *travel.EsSyncReq) (*travel.EsSyncResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.EsSync(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.EsSyncResp), nil
}

func EsSyncGet(ctx context.Context, i *travel.EsSyncGetReq) (*travel.EsSyncGetResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.EsSyncGet(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.EsSyncGetResp), nil
}
func AreaList(ctx context.Context, i *travel.AreaListReq) (*travel.AreaListResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.AreaList(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.AreaListResp), nil
}

func BuyTicKet(ctx context.Context, i *travel.BuyTicKetReq) (*travel.BuyTicKetResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.BuyTicKet(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.BuyTicKetResp), nil
}
func CallBack(ctx context.Context, i *travel.PayCallbackReq) (*travel.PayCallbackResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.PayCallback(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.PayCallbackResp), nil
}
func OrderList(ctx context.Context, i *travel.FindOrderReq) (*travel.FindOrderResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.FindOrder(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.FindOrderResp), nil
}
func OrderDel(ctx context.Context, i *travel.DelOrderReq) (*travel.DelOrderResp, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		sync, err := in.DelOrder(ctx, i)
		if err != nil {
			return nil, err
		}
		return sync, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.DelOrderResp), nil
}
